<template>
  <div class="main">
    <div class="title">
      <a href="/login">登录</a>
      <span>·</span>
      <a class="active" href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="params">
        <el-form-item
          class="input-prepend restyle"
          prop="nickname"
          :rules="[
            { required: true, message: '请输入你的昵称', trigger: 'blur' },
          ]"
        >
          <div>
            <el-input
              type="text"
              placeholder="你的昵称"
              v-model="params.nickname"
            />
            <i class="iconfont icon-user" />
          </div>
        </el-form-item>

        <!-- :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' }, { validator: checkPhone, trigger: 'blur' }]" -->
        <!-- 前面是设置的规则，checkPhone是自己设置规则的方法 -->
        <el-form-item
          class="input-prepend restyle no-radius"
          prop="mobile"
          :rules="[
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { validator: checkPhone, trigger: 'blur' },
          ]"
        >
          <div>
            <el-input
              type="text"
              placeholder="手机号"
              v-model="params.mobile"
            />
            <i class="iconfont icon-phone" />
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend"
          prop="password"
          :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
        >
          <div>
            <el-input
              type="password"
              placeholder="设置密码"
              v-model="params.password"
            />
            <i class="iconfont icon-password" />
          </div>
        </el-form-item>

        <el-form-item>
          <div>
            性别：
            <el-radio v-model="params.sex" label="1">男</el-radio>
            <el-radio v-model="params.sex" label="2">女</el-radio>
          </div>
        </el-form-item>

        <div class="btn">
          <input
            type="button"
            class="sign-up-button"
            value="注册"
            @click="submitRegister()"
          />
        </div>
        <p class="sign-up-msg">
          点击 “注册” 即表示您同意并愿意遵守简书
          <br />
          <a target="_blank" href="http://www.jianshu.com/p/c44d171298ce"
            >用户协议</a
          >
          和
          <a target="_blank" href="http://www.jianshu.com/p/2ov8x3">隐私政策</a>
          。
        </p>
      </el-form>
      <!-- 更多注册方式 -->
    </div>
  </div>
</template>
  
<script>
import "~/assets/css/sign.css";
import "~/assets/css/iconfont.css";

import registerApi from "@/api/register";

export default {
  layout: "sign",
  data() {
    return {
      params: {
        mobile: "",
        sex: "1", // 默认性别为1：男
        nickname: "",
        password: "",
      },
      //   sending: true, //是否发送验证码
      //   second: 60, //倒计时间
      //   codeTest: "获取验证码", //这一定要用差值表达式赋值，因为这样才能动态显示时间
    };
  },
  methods: {
    // 一点击注册按钮就执行这个方法
    submitRegister() {
      console.log("aaaaaaa!!!");
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          console.log("ok");
        }
        console("没有经过参数校验");
      });
      registerApi.userRegister(this.params).then((response) => {
        //提示注册成功
        this.$message({
          type: "success",
          message: "注册成功",
        });
        // 路由跳转到login页面
        this.$router.push({ path: "/login" });
      });
    },
    // 手机号检验的方法
    checkPhone(rule, value, callback) {
      //debugger
      // if (!(/^1[34578]\d{9}$/.test(value))) {
      //     return callback(new Error('手机号码格式不正确'))
      // }
      // return callback()
    },
  },
};
</script>
  